<template>
  <div class="no-auth-container">
    <a-result status="403" title="无权限访问" sub-title="您没有权限访问该页面">
      <template #extra>
        <div class="action-container">
          <p class="countdown">{{ countdown }} 秒后自动返回首页</p>
          <a-button type="primary" @click="goHome">立即返回首页</a-button>
        </div>
      </template>
    </a-result>
  </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const countdown = ref(5)
let timer: number | undefined

const goHome = () => {
  if (timer) {
    clearInterval(timer)
  }
  router.push('/')
}

onMounted(() => {
  timer = window.setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
      goHome()
    }
  }, 1000)
})

onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
  }
})
</script>

<style scoped>
.no-auth-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.countdown {
  color: rgba(0, 0, 0, 0.45);
  font-size: 14px;
  margin: 0;
}
</style>
